using System; 
using System.Net.Mail;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for MailClass
/// </summary>
public class MailProvider
{
    public MailProvider()
	{
        mailAddress = new
        MailAddress("admin@rcv.com");
    }
    MailAddress mailAddress = null;
    public MailProvider(string mailServerName)
    {
        hostServerName = mailServerName;
        mailAddress = new
        MailAddress("admin@rcv.com");
    } 
    
    private string hostServerName = "mail.rcv.com";
    public string HostServerName
    {
        set { 
                if(value == "")
                    hostServerName = "mail.rcv.com";
                else
                    hostServerName = value; 
            }
        get { return hostServerName; }
    }
     
    private string errorMessage = "";
    public string ErrorMessage
    {
        set { errorMessage = value; }
        get { return errorMessage; }
    }
    
	public bool SendMail( 
	string emailTo, string subject, string body)
	{         
        bool sendSucessful = true;
        try
        {
            MailMessage mailMessage = new MailMessage();    
            mailMessage.From = mailAddress;     
            mailMessage.To.Add(emailTo);
            mailMessage.Subject = subject;             
            mailMessage.Body = body;
            mailMessage.IsBodyHtml = true;
            mailMessage.Priority = MailPriority.High;
            SmtpClient smtpClient = 
            new SmtpClient(hostServerName);
            smtpClient.Send(mailMessage);
        }
	    catch(Exception ex)
	    {
            sendSucessful = false;
	        errorMessage=ex.Message;
	        
	    }
        return sendSucessful;
	}
	
    public bool SendMail( 
    string emailTo, string emailCC, 
    string emailBCC, string subject, string body)
    {       
        bool sendSucessful = true;
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = mailAddress;
            mailMessage.To.Add(emailTo);
            mailMessage.Subject = subject;
            if(emailCC != "")
                mailMessage.CC.Add(emailCC);
            if (emailBCC != "")
                mailMessage.Bcc.Add(emailBCC);
            mailMessage.Body = body;
            mailMessage.IsBodyHtml = true;
            mailMessage.Priority = MailPriority.High;
            SmtpClient smtpClient = 
            new SmtpClient(hostServerName);             
            smtpClient.Send(mailMessage);
        }
        catch (Exception ex)
        {
            sendSucessful = false;
            errorMessage = ex.Message;

        }
        return sendSucessful;
    }
    
    public bool SendMail( 
    string emailTo, string emailCC, 
    string emailBCC, string subject, 
    string body, string attachFileName)
    {         
        bool sendSucessful = true;
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = mailAddress;
            mailMessage.To.Add(emailTo);
            mailMessage.Subject = subject;
            if(emailCC != "")
                mailMessage.CC.Add(emailCC);
            if (emailBCC != "")
                mailMessage.Bcc.Add(emailBCC);
            mailMessage.Body = body;
            if (attachFileName != "")
            {
                Attachment attachment =
                new Attachment(attachFileName);
                mailMessage.Attachments.Add(attachment);
            }
            mailMessage.IsBodyHtml = true;
            mailMessage.Priority = MailPriority.High;
            SmtpClient smtpClient = 
            new SmtpClient(hostServerName);             
            smtpClient.Send(mailMessage);                               
        }
        catch (Exception ex)
        {
            sendSucessful = false;
            errorMessage = ex.Message;

        }
        return sendSucessful;
    }
    
    public bool SendMail(
    string emailTo, string emailCC,
    string emailBCC, string subject,
    string body, string[] attachFileName)
    {         
        bool sendSucessful = true;
        try
        {
           MailMessage mailMessage = new MailMessage();
           mailMessage.From = mailAddress;
            mailMessage.To.Add(emailTo);
            mailMessage.Subject = subject;
            if(emailCC != "")
                mailMessage.CC.Add(emailCC);
            if (emailBCC != "")
                mailMessage.Bcc.Add(emailBCC);
            mailMessage.Body = body;
            if (attachFileName != null)
            {
                foreach(string fileName in attachFileName)
                {
                    AttachFile(mailMessage, fileName);
                }                
            }
            mailMessage.IsBodyHtml = true;
            mailMessage.Priority = MailPriority.High;
            SmtpClient smtpClient = 
            new SmtpClient(hostServerName);
            smtpClient.Send(mailMessage);  
        }
        catch (Exception ex)
        {
            sendSucessful = false;
            errorMessage = ex.Message;

        }
        return sendSucessful;
    }
    
    void AttachFile(MailMessage mailMessage, 
    string  attachFileName)
    {
        if (attachFileName != "")
        {
            using (Attachment attachment =
                new Attachment(attachFileName))
            {                                                
                mailMessage.Attachments.Add(attachment);
            }            
        }
                  
    }

    public bool IsEmail(string inputEmail)
    {
        if (inputEmail == null | inputEmail.Length > 35) return false;
        string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
            @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
            @".)+))([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$";
        Regex re = new Regex(strRegex);
        return (re.IsMatch(inputEmail));
    }
}
